<?php
Class Controller_Manage_Pro extends Controller_Abstract
{
	function actionCreate()
	{
		$this->_view['title'] = '创建商品';
		$vegeprocat = VegeProCat::find()->asArray()->getAll();
		$this->_view['vegeprocat'] = $vegeprocat;
		//获取主体信息
		/*$exsit = VegePro::find()->order('number desc')->asArray()->getOne();
		if(isset($exsit['id'])&&$exsit['id'])
		{
			$number = ((int)substr($exsit['number'],-3))+1;
			$number = $baseinfo['companynumber'].'3'.str_pad($number,3,0,STR_PAD_LEFT);
		}
		else
		{
			$number = $baseinfo['companynumber'].'3001';
		}
		
		$this->_view['number'] = $number;*/
	}
	
	function actionCreateSave()
	{
		$this->_view['title'] = '创建商品保存';
		//dump($_POST);
		//echo '<br>';
		//dump($_FILES);
		//exit;
		/**
		Array
		(
			[type] => 1
			[number] => 000023
			[name] => 蔬菜
			[thumbnail] => C:\fakepath\wxzf.png
			[img] => C:\fakepath\微信支付.png
			[retailprice] => 1
			[category] => 蔬菜
			[isoffshelves] => 1
			[spec] => 箱
			[content] => <p>3123123&nbsp;</p>
		)


		Array
		(
			[file-thumbnail] => Array
				(
					[name] => wxzf.png
					[type] => image/png
					[tmp_name] => D:\wamp\tmp\phpBD9F.tmp
					[error] => 0
					[size] => 12077
				)

			[file-img] => Array
				(
					[name] => 微信支付.png
					[type] => image/png
					[tmp_name] => D:\wamp\tmp\phpBDB0.tmp
					[error] => 0
					[size] => 111904
				)

		)
		*/
		if($_FILES['file-thumbnail']['size']>0&&$_FILES['file-img']['size']>0)
		{
			$thumb = Helper_BSS_Normal::upload($_FILES['file-thumbnail'], Q::ini('custom_system/base_url').'upload/pro/thumb');//缩略图
			$img = Helper_BSS_Normal::upload($_FILES['file-img'], Q::ini('custom_system/base_url').'upload/pro/img');//大图
			if ($img['ack'] == SUCCESS && $thumb['ack'] == SUCCESS)
			{
				$thumbsrc = $thumb['data'];
				$imgsrc = $img['data'];
			}
			else
			{
				return $this->_redirectMessage('提交失败', "图片上传失败，请重新选择图片", $_SERVER['HTTP_REFERER'],FAILURE);
			}
		}
		
		$number = isset($_POST['number'])?$_POST['number']:'';
		$name = isset($_POST['name'])?$_POST['name']:'';
		$retailprice = isset($_POST['retailprice'])?$_POST['retailprice']:'';
		$category = isset($_POST['category'])?$_POST['category']:'';
		$isoffshelves = isset($_POST['isoffshelves'])?$_POST['isoffshelves']:'';
		$spec = isset($_POST['spec'])?$_POST['spec']:'';
		$content = isset($_POST['content'])?$_POST['content']:'';
		$time = date('Y-m-d H:i:s');
		
		if((!isset($thumbsrc)||!$thumbsrc)||(!isset($imgsrc)||!$imgsrc))
		{
			return $this->_redirectMessage('提交失败', "图片上传失败，请重新选择图片", $_SERVER['HTTP_REFERER'],FAILURE);
		}
		
		if($number && $name)
		{
			$exsit = VegePro::find('number=? or name=?',$number,$name)->asArray()->getOne();
			if(isset($exsit['id'])&&$exsit['id'])
			{
				return $this->_redirectMessage('提交失败', "对应名称或编号的商品信息已经存在", url('Manage::Pro/Create'),FAILURE);
			}
			
			$new = new VegePro();
			$new->number = $number;
			$new->name = $name;
			$new->category = $category;
			$new->content = $content;
			$new->retailprice = $retailprice;
			$new->isoffshelves = $isoffshelves;
			$new->time = $time;
			$new->thumbnail = $thumbsrc;
			$new->img = $imgsrc;
			$new->spec = $spec;
			$new->save();//终极方法
			return $this->_redirectMessage('提交成功', "正在返回..", url('Manage::Pro/Create'),SUCCESS);
		}
		else
		{
			return $this->_redirectMessage('提交失败', "编号和名称不能为空", url('Manage::Pro/Create'),FAILURE);
		}
	}
	
	function actionList()
	{
		$this->_view['title'] = '商品信息列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = VegePro::search($cp, $ps, $url_arr);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Pagec($result['pagination'], url('Manage::Pro/List', $url_arr));
			$this->_view['page'] = $page->getPage();
            $this->_view['pagecount'] = $result['pagination'];
			$this->_view['list'] = $result['data'];
		}
	}
	
	function actionDetail()
	{
		$this->_view['title'] = '商品信息详情';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$product = VegePro::find('id=?', $id)->asArray()->getOne();
		if ($product)
		{
			$this->_view['product'] =  $product;
		}
		else
		{
			return $this->_redirectMessage('查询失败', '提交参数错误', url('Manage::Pro/List'), FAILURE);
		}
	}
	
	function actionEdit()
	{
		$this->_view['title'] = '商品编辑';
		$vegeprocat = VegeProCat::find()->asArray()->getAll();
		$this->_view['vegeprocat'] = $vegeprocat;
		
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$vegepro = VegePro::find('id=?', $id)->asArray()->getOne();
		if ($vegepro)
		{
			$this->_view['vegepro'] = $vegepro;
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '商品不存在', url('Manage::Pro/List'), FAILURE);
		}
	}
	
	function actionEditSave()
	{
		$this->_view['title'] = '商品编辑保存';
		if (isset($_POST)&&$_POST)
		{
			if(isset($_FILES['file-thumbnail']['size'])&&$_FILES['file-thumbnail']['size']>0)
			{
				$thumb = Helper_BSS_Normal::upload($_FILES['file-thumbnail'], Q::ini('custom_system/base_url').'upload/pro/thumb');//缩略图
				if ($thumb['ack'] == SUCCESS)
				{
					$thumbsrc = $thumb['data'];
				}
				else
				{
					return $this->_redirectMessage('提交失败', "缩略图上传失败，请重新选择图片", $_SERVER['HTTP_REFERER'],FAILURE);
				}
			}
			
			if(isset($_FILES['file-img']['size'])&&$_FILES['file-img']['size']>0)
			{
				$img = Helper_BSS_Normal::upload($_FILES['file-img'], Q::ini('custom_system/base_url').'upload/pro/img');//大图
				if ($img['ack'] == SUCCESS)
				{
					$imgsrc = $img['data'];
				}
				else
				{
					return $this->_redirectMessage('提交失败', "大图上传失败，请重新选择图片", $_SERVER['HTTP_REFERER'],FAILURE);
				}
			}
			
			$id = isset($_POST['id'])?$_POST['id']:'';
			$number = isset($_POST['number'])?$_POST['number']:'';
			$name = isset($_POST['name'])?$_POST['name']:'';
			$retailprice = isset($_POST['retailprice'])?$_POST['retailprice']:'';
			$category = isset($_POST['category'])?$_POST['category']:'';
			$isoffshelves = isset($_POST['isoffshelves'])?$_POST['isoffshelves']:'';
			$spec = isset($_POST['spec'])?$_POST['spec']:'';
			$content = isset($_POST['content'])?$_POST['content']:'';
			$time = date('Y-m-d H:i:s');
			
			if($number && $name && $id)
			{
				$edit = VegePro::find('id=?', $id)->getOne();
				
				if(!isset($edit['id']) || !$edit['id'])
				{
					return $this->_redirectMessage('提交失败', "不存在该产品", $_SERVER['HTTP_REFERER'],FAILURE);
				}
				
				$edit->name = $name;
				$edit->category = $category;
				$edit->content = $content;
				$edit->retailprice = $retailprice;
				$edit->isoffshelves = $isoffshelves;
				$edit->time = $time;
				
				if(isset($thumbsrc)&&$thumbsrc)
				{
					$edit->thumbnail = $thumbsrc;
				}
				
				if(isset($imgsrc)&&$imgsrc)
				{
					$edit->img = $imgsrc;
				}
				
				$edit->spec = $spec;
				$edit->save();//终极方法
				
				return $this->_redirectMessage('提交成功', "正在返回..", $_SERVER['HTTP_REFERER'],SUCCESS);//只返回上一页即可，这样好定位
			}
			else
			{
				return $this->_redirectMessage('提交失败', "编号和名称不能为空", $_SERVER['HTTP_REFERER'],FAILURE);
			}
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '请检查提交的数据是否正确', url('Manage::Pro/List'), FAILURE);
		}
	}
	
	function actionDelete()
	{
		$this->_view['title'] = '删除';
		$result = VegePro::meta()->deleteWhere(array('id'=>$_GET['id']));
		if (!$result)
		{
            echo 1;exit;
		}
		else
		{
            echo 0;exit;
		}
	}
	
	function actionDeleteAll()
	{
		$this->_view['title'] = '勾选删除';
		if ($this->_context->isGET())
		{
			$ids = explode(',', $_GET['ids']);
			$msg = '';
			foreach ($ids as $i)
			{
				$result = VegePro::meta()->deleteWhere(array('id'=>$i));
				if (!$result)
				{
					$msg = '';
				}
				else
				{
					$msg = $i.'删除失败';
				}
			}

			if (!$msg)
			{
				return $this->_redirectMessage('批量删除成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
			}
			else
			{
				return $this->_redirectMessage('删除失败', '请检查提交的数据是否存在', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionStop()
    {
        $this->_view['title'] = '上下架';
        $result = VegePro::find(array('id'=>$_GET['id']))->getOne();
        if ($result)
        {
			if($result['isoffshelves']==1)
			{
				$result->isoffshelves = 2;
			}
			else
			{
				$result->isoffshelves = 1;
			}
            $result->save();
            echo 1;exit;
        }
        else
        {
            echo 0;exit;
        }
    }
	
	function actionExportBusinessBaseInfo()
	{
		$this->_view['title'] = '导出商品信息';
		set_time_limit(0);
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$vegesupplier = VegePro::find()->asArray()->getAll();
		//dump($businessbaseinfo);exit;
		if ($vegesupplier)
		{
			$baseinfo = BaseNodeInfo::find()->asArray()->getOne();
			
			$filename = $baseinfo['companynumber'] . '_KHDA.txt'; //设置文件名
			header('Content-Type: text/txt');
			header("Content-Disposition: attachment;filename={$filename}");
			$out = fopen('php://output', 'w');
			foreach($vegesupplier as $vs)
			{
				$line .= ",".$vs['suppliernumber'];
				$line .= ",".$vs['suppliername'];
				$line .= ",".$vs['regnum'];
				$line .= ",".str_replace('-','',$vs['recorddate']);
				$line .= ",".$vs['corporate'];
				$line .= ",".$vs['phone'];
				$line .= ",".str_replace('-','',$vs['updatetime']);
				fputs($out, $line."\r\n");
			}
			
			fclose($out);
		}
		else
		{
			return $this->_redirectMessage('导出失败', '没有任何信息', url('Manage::Pro/List'), FAILURE);
		}
	}
}